package fr.xebia.view;

import java.util.Collections;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

import fr.xebia.data.Contract;
import fr.xebia.data.ContractType;
import fr.xebia.service.ContractService;
import fr.xebia.service.ContractTypeService;
import fr.xebia.service.ServiceException;

/**
 * 
 */
public class ContratAction extends GenericAction implements ModelDriven<Contract> {

	private static final long serialVersionUID = 1L;

	private ContractService contratService;

	private Contract contrat;

	public Contract getContrat() {
		return contrat;
	}

	/**
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public Contract getModel() {
		contrat = new Contract();
		return contrat;
	}

	/**
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		logger.debug("Preparing " + getClass().getName());
		contratService = serviceLocator.getContractService();
		addContratTypes();
	}

	/**
	 * @throws ServiceException
	 */
	protected void addContratTypes() throws Exception {
		ContractTypeService contratTypeService = serviceLocator.getContractTypeService();
		List<ContractType> contratTypes = contratTypeService.list();
		request.setAttribute("contratTypes", contratTypes);
	}

	public String prepareadd() throws Exception {
		return Action.SUCCESS;
	}

	public String list() throws Exception {
		try {
			List<Contract> contrats = contratService.list();
			request.setAttribute("contrats", contrats);
			logger.debug("Found " + contrats.size() + " contrats");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("contrats", Collections.EMPTY_LIST);
			return handleError("Can't get contrats", e);
		}
	}

	public String search() throws Exception {
		try {
			List<Contract> contrats = contratService.search(contrat);
			request.setAttribute("contrats", contrats);
			logger.debug("Found " + contrats.size() + " contrats");
			return Action.SUCCESS;
		} catch (Exception e) {
			request.setAttribute("contrats", Collections.EMPTY_LIST);
			return handleError("Can't get contrats", e);
		}
	}

	public String add() throws Exception {
		try {
			logger.info("Adding new contrat");
			Long id = (Long) contratService.save(contrat);
			contrat.setId(id);
			addActionMessage("Added new contrat with id: " + contrat.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't add contrat", e);
		}
	}

	public String update() throws Exception {
		try {
			logger.info("Updating contrat with id: " + contrat.getId());
			contratService.update(contrat);
			addActionMessage("Updated contrat with id: " + contrat.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't save contrat", e);
		}
	}

	public String edit() throws Exception {
		try {
			logger.info("Getting contrat with id: " + contrat.getId());
			contrat = contratService.get(contrat.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't get contrat", e);
		}
	}

	public String delete() throws Exception {
		try {
			logger.info("Deleting contrat with id: " + contrat.getId());
			contratService.deleteById(contrat.getId());
			addActionMessage("Deleted contrat with id: " + contrat.getId());
			return Action.SUCCESS;
		} catch (Exception e) {
			return handleError("Can't delete contrat", e);
		}
	}
}
